package bookmanagementsystem.gui;

import bookmanagementsystem.dao.BookDAO;
import bookmanagementsystem.model.Book;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ModifyBookInfoFrame extends JFrame {
    private JTextField bookIDField;
    private JTextField bookNameField;
    private JTextField authorNameField;
    private JTextField quantityInLibraryField;
    private JTextField locationField;
    private JButton confirmButton;

    public ModifyBookInfoFrame() {
        setTitle("修改图书信息");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        initComponents();
        addComponentsToFrame();
        addActionListeners();
    }

    private void initComponents() {
        bookIDField = new JTextField(20);
        bookNameField = new JTextField(20);
        authorNameField = new JTextField(20);
        quantityInLibraryField = new JTextField(20);
        locationField = new JTextField(20);
        confirmButton = new JButton("确认修改");
    }

    private void addComponentsToFrame() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.add(new JLabel("图书ID（不可修改）："));
        panel.add(bookIDField);
        panel.add(new JLabel("图书名称："));
        panel.add(bookNameField);
        panel.add(new JLabel("作者："));
        panel.add(authorNameField);
        panel.add(new JLabel("库存数量："));
        panel.add(quantityInLibraryField);
        panel.add(new JLabel("存放位置："));
        panel.add(locationField);
        panel.add(confirmButton);

        getContentPane().add(panel);
    }

    private void addActionListeners() {
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String bookID = bookIDField.getText();
                String bookName = bookNameField.getText();
                String authorName = authorNameField.getText();
                int quantityInLibrary;
                try {
                    quantityInLibrary = Integer.parseInt(quantityInLibraryField.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(ModifyBookInfoFrame.this, "库存数量必须是整数，请重新输入。");
                    return;
                }
                String location = locationField.getText();

                Book book = new Book(bookID, bookName, authorName, null, quantityInLibrary, location);
                BookDAO bookDAO = new BookDAO();
                if (bookDAO.updateBookInfo(book)) {
                    JOptionPane.showMessageDialog(ModifyBookInfoFrame.this, "图书信息修改成功！");
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(ModifyBookInfoFrame.this, "图书信息修改失败，请检查输入信息或联系管理员。");
                }
            }
        });
    }
}